<template>
  <div class="message-list" ref="messageList">
    <div v-if="isLoading" class="loading">加载中...</div>
    <div v-else-if="messages.length === 0" class="empty-state">
      还没有消息，开始聊天吧
    </div>
    <div v-else>
      <div 
        v-for="message in messages" 
        :key="message.id" 
        :class="['message', { 'own-message': message.sender.id === authStore.user.id }]"
      >
        <img v-if="message.sender.id !== authStore.user.id" :src="message.sender.avatar" class="avatar" />
        <div class="message-content">
          <div class="message-bubble">
            <div class="message-text">{{ message.content }}</div>
            <div class="message-time">{{ formatTime(message.sentAt) }}</div>
          </div>
        </div>
      </div>
      <div v-if="isTyping" class="typing-indicator">
        <div class="typing-dots">
          <span></span>
          <span></span>
          <span></span>
        </div>
        正在输入...
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, nextTick, onMounted } from 'vue'
import { useChatStore } from '@/stores/chat'
import { useAuthStore } from '@/stores/auth'

const chatStore = useChatStore()
const authStore = useAuthStore()
const messageList = ref(null)

const messages = computed(() => chatStore.messages)
const isLoading = computed(() => chatStore.isLoading)
const isTyping = computed(() => chatStore.isTyping)

// 滚动到底部
const scrollToBottom = () => {
  if (messageList.value) {
    nextTick(() => {
      messageList.value.scrollTop = messageList.value.scrollHeight
    })
  }
}

// 监听消息变化，自动滚动
watch(messages, scrollToBottom, { deep: true })

// 监听当前聊天变化
watch(() => chatStore.currentChat, scrollToBottom)

onMounted(() => {
  scrollToBottom()
})

const formatTime = (time) => {
  const date = new Date(time)
  return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
}
</script>

<style scoped>
.message-list {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
  background-color: #f5f5f5;
}

.loading,
.empty-state {
  text-align: center;
  color: #999;
  padding: 2rem;
}

.message {
  display: flex;
  margin-bottom: 1rem;
}

.message.own-message {
  flex-direction: row-reverse;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  margin-right: 0.5rem;
}

.message.own-message .avatar {
  margin-right: 0;
  margin-left: 0.5rem;
}

.message-content {
  max-width: 70%;
}

.message-bubble {
  background-color: white;
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.message.own-message .message-bubble {
  background-color: #95ec69;
}

.message-text {
  margin-bottom: 0.25rem;
}

.message-time {
  font-size: 0.75rem;
  color: #999;
  text-align: right;
}

.typing-indicator {
  display: flex;
  align-items: center;
  color: #999;
  font-size: 0.875rem;
  margin-top: 0.5rem;
}

.typing-dots {
  display: flex;
  margin-right: 0.5rem;
}

.typing-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background-color: #999;
  margin: 0 1px;
  animation: typing 1.4s infinite both;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}
</style>